<!-- 编辑用户 -->
<template>
  <Dialog v-model="dialogShow" :title="dialogTitle" width="840px" @confirm="confirm" @close="close" :show-close="false">
    <template #header="{ close, titleId, titleClass }">
      <div class="use-dialog-header">
        <div :id="titleId" :class="titleClass">配置项</div>
        <div class="close">
          <el-icon @click="close">
            <CloseBold />
          </el-icon>
        </div>
      </div>
    </template>
    <div class="main">
      <el-form ref="formRef" :model="formData" :rules="rules" label-position="top">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="编码" prop="code">
              <el-input v-model="formData.code" disabled placeholder="自动生成"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="名称" prop="name">
              <el-input v-model="formData.name" placeholder="请输入名称"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template v-if="rowData">
        <div class="use-title-icon">列表</div>
        <ProTable
          ref="tableRef"
          :columns="columns"
          :data="tableData"
          maxHeight="400px"
          :pagination="false"
          border
          @darg-sort="sortTable"
        >
          <!-- 表格操作 -->
          <template #operation="scope">
            <el-button type="warning" v-if="scope.row.edit" link @click="saveFn(scope.row, rowData.id)">保存</el-button>
            <el-button type="warning" v-else link @click="editFn(scope)">修改</el-button>
            <el-button type="warning" link @click="delFn(scope)">删除</el-button>
          </template>
        </ProTable>
        <el-button type="warning" link @click="addRowFn">添加名称</el-button>
      </template>
    </div>
  </Dialog>
</template>

<script setup lang="jsx">
import { useHandleData } from "@/hooks/useHandleData";

import api from "@/api";
const dialogTitle = ref("");
const dialogShow = ref(false);
const formRef = ref();
const rowData = ref();
const categoryId = ref();
const tableRef = ref();
const tableData = ref([]);

const formData = ref({
  code: "",
  name: "",
});
const rules = {
  name: { required: true, message: "请输入名称", trigger: "blur" },
};

const getTableList = () => {
  api
    .post(
      `/serviceItem/queryList`,
      {
        parentId: rowData.value.id,
      },
      {
        loading: true,
      }
    )
    .then((res) => {
      tableData.value = res;
    });
};

// 表格配置项
const columns = ref([
  // { type: "sort", label: "排序", width: 80 },
  { label: "编码", prop: "code", align: "left" },
  {
    label: "名称",
    prop: "name",
    align: "left",
    render: ({ row, column, $index }) => {
      if (row.edit) {
        return <el-input vModel={row[column.property]} />;
      } else {
        return row[column.property];
      }
    },
  },
  { label: "修改时间", prop: "updateTime", align: "left" },
  { prop: "operation", label: "操作", width: "120px", align: "center", fixed: "right" },
]);

const show = (row, id) => {
  dialogShow.value = true;
  categoryId.value = id;
  rowData.value = row;
  if (row) {
    formData.value.code = row.code;
    formData.value.name = row.name;
    formData.value.id = row.id;
    getTableList();
  }
};

const close = () => {
  rowData.value = "";
  formRef.value.resetFields();
  dialogShow.value = false;
  emit("updata");
};

const addRowFn = () => {
  tableData.value.push({
    edit: true,
    name: "",
  });
};
const editFn = (scope) => {
  scope.row.edit = true;
};

const delFn = async ({ row, $index }) => {
  if (row.id) {
    await useHandleData(() => api.get(`/serviceItem/delete/${row.id}`, {}, { loading: true }), `删除【${row.name}】`);
    getTableList();
  } else {
    tableData.value.splice($index, 1);
  }
};

// 表格拖拽排序
const sortTable = ({ newIndex, oldIndex }) => {
  console.log(newIndex, oldIndex);
  console.log(proTable.value?.tableData);
  ElMessage.success("修改列表排序成功");
};

const confirm = () => {
  formRef.value.validate((valid, fields) => {
    if (valid) {
      saveFn(formData.value, 0, close);
    } else {
      console.log("error submit!", fields);
    }
  });
};

// 保存
const saveFn = (data, parentId, callback) => {
  let url = data.id ? "/serviceItem/update" : "/serviceItem/add";
  api
    .post(
      url,
      {
        name: data.name,
        categoryId: categoryId.value,
        // sort: data.sort || tableData.value.length - 1,
        id: data.id,
        parentId,
      },
      { msg: true }
    )
    .then(() => {
      if (callback) {
        callback();
      } else {
        getTableList();
      }
    });
};

const emit = defineEmits(["updata"]);
defineExpose({ show });
</script>
<style lang="scss" scoped>
.captcha {
  flex: 1;
  margin-right: 10px;
}

.toggle {
  .el-button--text {
    color: var(--use-color-warning);
    font-size: 16px;
  }
}

.btns {
  margin: 32px 170px;

  .el-button {
    width: 100%;
  }
}

.form {
  .label {
    display: inline-flex;
    align-items: center;

    .num {
      width: 18px;
      height: 18px;
      line-height: 18px;
      border-radius: 2px;
      background: var(--use-color-light-2);
      text-align: center;
      font-size: 12px;
      margin-right: 8px;
    }
  }
}

::v-deep(.table-main) {
  margin-top: 0;
  padding: 0;
}
</style>
